package leetcode.top100;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author ：zsy
 * @date ：Created 2022/11/30 9:07 PM
 * @description：
 */
public class _046_全排列 {
    @Test
    public void test() {
        List<List<Integer>> lists = new Solution().permute(new int[]{1, 2, 3});
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
    }

    class Solution {
        List<List<Integer>> res = new ArrayList<>();
        public List<List<Integer>> permute(int[] nums) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int num : nums) {
                list.add(num);
            }
            traceback(list, 0);
            return res;
        }

        private void traceback(ArrayList<Integer> list, int first) {
            if (first == list.size()) {
                res.add(new ArrayList<>(list));
                return;
            }
            for (int i = first; i < list.size(); i++) {
                Collections.swap(list, first, i);
                traceback(list, first + 1);
                Collections.swap(list, first, i);
            }
        }
    }
}
